package org.zero.controller;

import com.alipay.api.internal.util.AlipaySignature;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zero.config.AlipayProperties;

import java.util.Map;

/**
 * @author Zero (cnzeropro@qq.com)
 * @date 2021/11/1 19:14
 */
@Slf4j
@RestController
@RequestMapping("alipay")
public class AlipayController {

    @SneakyThrows
    @GetMapping("return")
    public String alipayReturn(@RequestParam Map<String, String> params) {
        /* {
         *  charset=utf-8,
         *  out_trade_no=cs189032174000012,
         *  method=alipay.trade.page.pay.return,
         *  total_amount=0.10,
         *  sign=NIyGZ4wJOHH7GsR5dToPMFw8fM6onh/AFgI7wzfe/BXn0sLRLlXmDJMT+QN48sjL1qz6Ynjy2GIf4F+e63xLhjWfFLR4+G9BVqZDHpwH8j9sX7R6Q3XcFl5t1/Eu/RngkSViB7JoUYyV5jvGEHtE58ZntMj0r9tVw+2ZfG8oDMSATeZFj+F0isVdY6QmArOBw+/FOdo+GFPNZICCl9JC9AV8ersmgZ8mc9Dn28f5BIEbe36WUQ29wGEJChQOTZED+fw6b8+kkyYFOIqSw0GVZnvCDvGCu9rcToyAKN4ER+3BNqSM+K1EUcDz7vCqYBDnbHAywCkLdZiS8xRH5QwH6Q==,
         *  trade_no=2021110122001427210501642064,
         *  auth_app_id=2021000118627008,
         *  version=1.0,
         *  app_id=2021000118627008,
         *  sign_type=RSA2,
         *  seller_id=2088621956519231,
         *  timestamp=2021-11-01 21:16:19
         * }
         */
        log.info("params: {}", params);

        // 验证签名
        boolean verifyResult = AlipaySignature.rsaCheckV1(params, AlipayProperties.publicKey, AlipayProperties.charset, AlipayProperties.signType);
        if (!verifyResult) {
            // 验签失败
            log.info("同步验签失败");
            return "同步验签失败";
        }
        // 验签成功
        log.info("同步验签成功");
        return "同步验签成功";
    }

    @SneakyThrows
    @PostMapping("notify")
    public String alipayNotify(@RequestBody Map<String, String> params) {
        /* {
         *  gmt_create=2021-11-01 21:16:04,
         *  charset=utf-8,
         *  gmt_payment=2021-11-01 21:16:10,
         *  notify_time=2021-11-01 21:16:12,
         *  subject=测试商品,
         *  sign=hi6kJkcYNQ5QUxurwAOM+UNlvi6KWV6GjmDwcMh8NKtL2MvSXAodsHFoCBhYYpEf12975cLCe01UDKU7a6d0QqiyYBlEs2UZfKRZl08TRL+GXg/I1TW71ShLIbjUGYJErg9uJY1a6XCDecD/PRnQbYUJ3nVPnebiJjBGeCwhj6NjbMGI9nRWM4lGhqvj4baKenbw2yyig7BBpIJCuL06tuqC+bxi/6L2QWFjuK64d3G1jgGv7Cy3oEiInzc9ho6ANesFJuZObngLFs+JQZzJ1YLJ9ctfUZMIIg4ybTi+TOVOrow9hoyPvhIZdjLtvY1kG4Rpr3DkAyuriPjcSRUrxg==,
         *  buyer_id=2088622956727215,
         *  invoice_amount=0.10,
         *  version=1.0,
         *  notify_id=2021110100222211611027210515603748,
         *  fund_bill_list=[{
         *      "amount":"0.10",
         *      "fundChannel":"ALIPAYACCOUNT"}],
         *  notify_type=trade_status_sync,
         *  out_trade_no=cs189032174000012,
         *  total_amount=0.10,
         *  trade_status=TRADE_SUCCESS,
         *  trade_no=2021110122001427210501642064,
         *  auth_app_id=2021000118627008,
         *  receipt_amount=0.10,
         *  point_amount=0.00,
         *  app_id=2021000118627008,
         *  buyer_pay_amount=0.10,
         *  sign_type=RSA2,
         *  seller_id=2088621956519231
         * }
         */
        log.info("params: {}", params);

        // 验证签名
        boolean verifyResult = AlipaySignature.rsaCheckV1(params, AlipayProperties.publicKey, AlipayProperties.charset, AlipayProperties.signType);
        // 获取交易状态
        String tradeStatus = params.get("trade_status");
        if (!verifyResult) {
            log.info("异步验签失败");
            return "异步验签失败";
        }
        log.info("异步验签成功");

        if ("TRADE_FINISHED".equals(tradeStatus)) {
            // 交易完结
        } else if ("TRADE_SUCCESS".equals(tradeStatus)) {
            // 支付成功
        } else if ("TRADE_CLOSED".equals(tradeStatus)) {
            // 交易关闭
        } else if ("WAIT_BUYER_PAY".equals(tradeStatus)) {
            // 交易创建
        } else {
            // 扩展用，现阶段不会进入
        }
        return "异步验签成功";
    }
}
